﻿using System;
using System.IO;
using System.Data;
using System.Text;
using MesInterface;
using System.Windows.Forms;

namespace Base.Core
{
    public class MesInterface
    {
        #region ___字段___
        private static readonly string Uri = @"https://mesrdweb.djicorp.com/BrowserWCFService/MobileService.asmx?WSDL";
        #endregion

        #region ___接口方法___
        /// <summary>
        /// 获取测试测试
        /// </summary>
        /// <param name="pcName"></param>
        /// <param name="Sn"></param>
        public static DataSet GetSNTestData(string Sn, out int result, out string returnMessage)
        {
            string pcName = GetPCName();
            MesOp local = new MesOp();
            int returnvalue = 0;
            string sendbuffer = "";
            string returnmessage = "";
            DataSet dataSetString = null;
            try
            {
                local.Init();
                local.SetUrl(Uri);
                local.SetSn(Sn);
                local.SetPcName(pcName);
                local.SetInterfaceName("GetSNTestData");
                local.GetSNTestData(out returnvalue, out returnmessage, out sendbuffer, out dataSetString);
                LogHelper.Log.Info("GetSNTestData -> returnMessage:" + returnmessage);
                LogHelper.Log.Info("GetSNTestData -> dataSet:" + dataSetString);
                local.Close();
            }
            catch (Exception ex)
            {
                LogHelper.Log.Error("GetSNTestData -> sendbuffer:" + ex.Message);
            }
            finally
            {
                LogHelper.Log.Info("GetSNTestData -> sendbuffer:" + sendbuffer);
            }
            returnMessage = returnmessage;
            result = returnvalue;
            return dataSetString;
        }

        /// <summary>
        /// 获取绑定关系
        /// </summary>
        /// <param name="Sn"></param>
        /// <returns></returns>
        public static DataSet Gett_SN_BindInfoALLSN(string Sn, out int result, out string returnMessage)
        {
            MesOp local = new MesOp();
            int returnvalue = 0;
            string returnmessage = "";
            string sendbuffer = "";
            DataSet dataSetString = null;
            local.Init();
            local.SetInterfaceName("Gett_SN_BindInfoALLSN");
            local.SetUrl(Uri);
            //必填 （接口获取，通过本机电脑名获取PCName,）
            local.SetAssembleSN(Sn);
            // returnvalue = -1 请求失败
            local.Gett_SN_BindInfoALLSN(out returnvalue, out returnmessage, out sendbuffer, out dataSetString);
            LogHelper.Log.Info("Gett_SN_BindInfoALLSN -> returnMessage:" + returnmessage);
            LogHelper.Log.Info("Gett_SN_BindInfoALLSN -> dataSet:" + dataSetString);
            local.Close();
            returnMessage = returnmessage;
            result = returnvalue;
            return dataSetString;
        }

        /// <summary>
        /// 上传测试结果
        /// </summary>
        /// <param name="pcName"></param>
        /// <param name="Sn"></param>
        /// <param name="optName"></param>
        /// <param name="testResult">pass-1,NG-2</param>
        /// <param name="defectCode"></param>
        /// <param name="isReTest">0-过站，1-重测</param>
        /// <param name="testTime"></param>
        public static string UploadResult(string Sn, string testResult, string isReTest, string stationId, out int result)
        {
            MesOp local = new MesOp();
            int returnvalue = 0;
            string returnmessage = "";
            string sendbuffer = "";
            try
            {
                DataSet dataSetString = null;
                string pcName = GetPCName();
                local.SetInterfaceName("DJI_DCTestLogDate_pro");
                local.Init();
                local.SetUrl(Uri);
                local.SetLotsn(Sn);
                local.SetResourceName(pcName);
                local.SetOperator("testop");
                local.SetTestResult(testResult);
                //1-988 2-500000000
                var defectCode = testResult.Equals("1") ? "988" : "500000000";
                local.SetDefectCode(defectCode);
                local.SetIsReTest(isReTest);
                local.SetNPIStationID(stationId);
                local.UploadResult(out returnvalue, out returnmessage, out sendbuffer, out dataSetString);
                LogHelper.Log.Info("UploadResult -> returnMessage:" + returnmessage);
                LogHelper.Log.Info("UploadResult -> dataSet:" + dataSetString);
                local.Close();
            }
            catch (Exception ex)
            {
                LogHelper.Log.Error("UploadResult -> sendbuffer:" + ex.Message);
            }
            finally
            {
                LogHelper.Log.Info("UploadResult -> sendbuffer:" + sendbuffer);
            }
            result = returnvalue;
            return returnmessage;
        }

        /// <summary>
        /// 获取本机电脑名
        /// </summary>
        /// <returns></returns>
        private static string GetPCName()
        {
            return SystemInformation.ComputerName;
        }
        #endregion
    }
}
